<script setup>
import { Edit, Delete } from '@element-plus/icons-vue'
import { ref } from 'vue'
import ChannelSelect from './components/ChannelSelect.vue'
import { artGetAtricleListService } from '@/api/article'
import { formatTime } from '@/utils/format'
import EditArticle from './components/EditArticle.vue'
const articleList = ref([])
const totalNum = ref(0)
const loading = ref(false)
const editArtRef = ref()
//添加文章
const onAddArticle = () => {
  editArtRef.value.open({})
}
//编辑逻辑
const onEditArticle = (row) => {
  // console.log(row)
  editArtRef.value.open({ row })
}
//删除逻辑
const onDeleteArticle = (row) => {
  console.log(row)
}
//搜索逻辑
const onSearch = () => {
  //回到第一页展示
  query.value.pagenum = 1
  getArticleList()
}
//重置逻辑
const onReset = () => {
  query.value.pagenum = 1
  query.value.cate_id = ''
  query.value.state = ''
  getArticleList()
}

//表单数据对象 / 请求参数对象
const query = ref({
  pagenum: 1, //当前页码
  pagesize: 5, //每页显示条数
  cate_id: '',
  state: '',
})
//获取渲染文章列表
const getArticleList = async () => {
  loading.value = true
  const res = await artGetAtricleListService(query.value)
  loading.value = false
  articleList.value = res.data.data
  totalNum.value = res.data.total
}
getArticleList()
//切换每页显示条数
const handleSizeChange = (size) => {
  query.value.pagenum = 1
  query.value.pagesize = size
  getArticleList()
}
//跳转页码
const handleCurrentChange = (page) => {
  query.value.pagenum = page
  getArticleList()
}
const handleSuccess = (key) => {
  if (key === 'add') {
    //回显最后一页的数据
    const lastPage = Math.ceil(totalNum.value / query.value.pagesize)
    query.value.pagenum = lastPage
  }
  getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">发布文章</el-button>
    </template>
    <!-- 表单区域 -->
    <el-form inline>
      <el-form-item label="文章分类">
        <ChannelSelect v-model="query.cate_id"></ChannelSelect>
      </el-form-item>
      <el-form-item label="发布状态">
        <el-select style="width: 150px" v-model="query.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!--表格区域 -->
    <el-table v-loading="loading" :data="articleList">
      <el-table-column label="文章标题" prop="title">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">
            {{ row.title }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            circle
            plain
            type="primary"
            :icon="Edit"
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            circle
            plain
            type="danger"
            :icon="Delete"
            @click="onDeleteArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      v-model:current-page="query.pagenum"
      v-model:page-size="query.pagesize"
      :page-sizes="[2, 3, 5, 10]"
      :background="true"
      layout=" jumper,total, sizes, prev, pager, next"
      :total="totalNum"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="justify-content: flex-end; margin-right: 50px; margin-top: 30px"
    />
    <!-- 抽屉区域 -->
    <EditArticle ref="editArtRef" @success="handleSuccess"></EditArticle>
  </page-container>
</template>
